package com.zl.gmadmin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zl.gmadmin.common.ApiResponse;
import com.zl.gmadmin.entity.SysDict;
import com.zl.gmadmin.payload.DictQueryCriteria;
import com.zl.gmadmin.service.SysDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhangliang
 * @version 1.0
 * @date 2021/4/12 10:07
 */
@Api(tags = "系统:数据字典")
@RequestMapping("/api/system/dict")
@RestController
public class SysDictController {

    private final SysDictService sysDictService;

    public SysDictController(SysDictService sysDictService) {
        this.sysDictService = sysDictService;
    }

    @GetMapping("/name")
    @ApiOperation("根据姓名获取数据字典")
    public ApiResponse findDictByName(DictQueryCriteria dictQueryCriteria){
//        LogUtil.debug("数据字典查询参数:"+JSONUtil.toJsonStr(dictQueryCriteria));

        return  ApiResponse.ofSuccess(sysDictService.findDictByName(dictQueryCriteria));
    }


    @GetMapping
    @ApiOperation("分页获取数据字典")
    public ApiResponse findDictByPage(DictQueryCriteria dictQueryCriteria,Page<SysDict> sysDictPage){
        return ApiResponse.ofSuccess(sysDictService.getDictByPage(dictQueryCriteria,sysDictPage));
    }

    @PostMapping
    @ApiOperation("增加数据字典")
    public ApiResponse addDict(@RequestBody SysDict sysDict){
        sysDictService.saveDict(sysDict);
        return ApiResponse.ofSuccess();
    }

    @PutMapping
    @ApiOperation("修改数据字典")
    public ApiResponse updateDict(@RequestBody SysDict sysDict){
        sysDictService.updateDictById(sysDict);
        return ApiResponse.ofSuccess();
    }

    @DeleteMapping
    @ApiOperation("删除数据字典")
    public ApiResponse deleteDict(@RequestBody List<Long> ids){
        sysDictService.deleteDictByIds(ids);
        return ApiResponse.ofSuccess();
    }
}
